﻿using System;
using System.Threading;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Navigation;

//using SBPweb.Forge.Controls.Silverlight.Utilities;

using SBPweb.RIA.Shared.Contracts.Controllers;
using SBPweb.RIA.Shared.Contracts.Models;

using SBPweb.RIA.Client.Contracts.Base;
using SBPweb.RIA.Client.Contracts.Controllers;
using SBPweb.RIA.Client.Contracts.Models;

using SBPweb.RIA.Client.Controls.Base;
using SBPweb.RIA.Client.Controls.Models;

namespace SBPweb.RIA.Client.Controls.Controllers
{

    public class NavigationController : ControllerBase<INavigationModel, NavigationModel, INavigationController>, INavigationController
    {

        #region Constructor logic

        public NavigationController()
        {
            Model = new NavigationModel();
        }

        #endregion

        #region Property accessors

        internal protected NavigationModel ModelTyped
        {
            get
            {
                return Model as NavigationModel;
            }
        }
                
        #endregion

        #region Methods

        public static void RegisterApplicationSingleton()
        {
            RegisterController(new NavigationController());
        }


        public bool NavigateToRootPage()
        {
            ExtendableApplication ea = Application as ExtendableApplication;
            string navigate = BrowserModel.GetQueryParameter("navigate");
            string currentCulture = BrowserModel.GetQueryParameter("currentCulture");

            if (string.IsNullOrEmpty(navigate))
            {
                return false;
            }
            if (string.IsNullOrEmpty(currentCulture))
            {
                currentCulture = "hu-HU";
            }
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);
            if (ea.IsRunningOutOfBrowser)
            {
                navigate = "/Views/Home.xaml";
            }
            //string type = ea.InitParams.Get<string>("type");
            //string instance = ea.InitParams.Get<string>("instance");

            //if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(instance))
            //{
            //    bool ret = Navigate("/Views/ExternalPage.xaml");
            //    if (NavigatingToExternalPackage != null)
            //    {
            //        NavigatingToExternalPackage(this, 
            //    }
            //    //    return NavigateToExternalForm(navigatePackage, navigateForm);
            //    return ret;
            //}
            //else
            //{
            return Navigate(navigate);
            //}
        }

        public bool Navigate(string uri)
        {
            return Navigate(new Uri(uri, UriKind.RelativeOrAbsolute));
        }

        public bool Navigate(Uri uri)
        {
            return ModelTyped.OnNavigation(uri);
        }

        //public bool NavigateToExternalForm(string packageName, string formPath)
        //{
        //    PackageLoaderController.Model.LoadPackage(packageName, formPath, new EventHandler<PackageLoaderEventArgs>(NavigatingToExternalPackage));
        //    //PackageLoader pl = new PackageLoader(this, packageName, formPath);
        //    return ret;
        //}

        //private void s(object sender, PackageLoaderEventArgs e)
        //{
        //    Navigate(
        //    //Navigate(string.Format("/{0};component/{1}", e.PackageName, e.FormPath));
        //}


        public void RaiseNavigating(Uri url, NavigationMode mode, bool cancel)
        {
            ModelTyped.OnNavigating(url, mode, cancel);
        }

        public void RaiseNavigated(Uri url, object content)
        {
            ModelTyped.OnNavigated(url, content);
        }

        public void GoBack()
        {
            ModelTyped.NavigationService.GoBack();
        }

        public void GoForward()
        {
            ModelTyped.NavigationService.GoForward();
        }

        #endregion
                
    }

}